/**
 * Created by lyf on 2017/6/20.
 */

'use strict'

let node_ssh = require('node-ssh');
const serviceConfig = require('../conf/service.json');
let ssh = new node_ssh();
let tsungMasterConf = require ('../conf/tsungMaster.json');
const fs = require ('fs');


module.exports = async function generateConf (serviceInfo) {

    tsungMasterConf.master = serviceInfo.tsungMaster[0][0];
    for (let i = 0; i < serviceInfo.tsungSlave.length; i++){
        tsungMasterConf.cleintArry.push({
            "host":`c${i+1}`,
            "weight":"1",
            "cpu":"8",
            "maxusers":"40000"
        });
        tsungMasterConf.monitorArry.push({
            "host":`c${i+1}`,
            "ip":serviceInfo.tsungSlave[i][0],
            "type":"erlang"
        });
    }

    if(serviceInfo.slb){
        for(let i = 0; i < serviceInfo.slb.length; i++ ){
            tsungMasterConf.servertArry.push({
                "ip":`${serviceInfo.slb[i][0]}`,
                "port":"8000",
                "type":"tcp"
            });
        }
    }

    for (let i = 0; i < serviceInfo.kong.length; i++){
        if(!serviceInfo.slb){
            tsungMasterConf.servertArry.push({
                "ip":`${serviceInfo.kong[i][0]}`,
                "port":"8000",
                "type":"tcp"
            });
        }

        tsungMasterConf.monitorArry.push({
            "host":`k${i+1}`,
            "ip":serviceInfo.kong[i][0],
            "type":"erlang"
        })
    }

    for (let i=0 ; i < serviceInfo.accesspoint.length; i++){
        tsungMasterConf.monitorArry.push({
            "host":`a${i+1}`,
            "ip":serviceInfo.accesspoint[i][0],
            "type":"erlang"
        })
    }

    for (let i=0 ; i < serviceInfo.kafka.length; i++){
        tsungMasterConf.monitorArry.push({
            "host":`kafka${i+1}`,
            "ip":serviceInfo.kafka[i][0],
            "type":"erlang"
        })
    }

    fs.writeFileSync (`${__dirname}/../result/tsungConf.json`, JSON.stringify(tsungMasterConf));



}